package cn.cuiliangpeng.org.controller;

import cn.cuiliangpeng.basic.utils.PageList;
import cn.cuiliangpeng.basic.utils.AjaxResult;
import cn.cuiliangpeng.org.domain.Employee;
import cn.cuiliangpeng.org.mapper.EmployeeMapper;
import cn.cuiliangpeng.org.query.EmployeeQuery;
import cn.cuiliangpeng.org.service.impl.EmployeeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门接口
 */
@RestController  //@Controller+@ResponseBody
@RequestMapping("/employee")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private EmployeeServiceImpl employeeService;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 查询单个部门
     * @param id
     * @return
     */
    @ApiOperation(value = "通过ID查询部门" )
    @GetMapping("/{id}")   //Get  http://localhost/employee/1
    public Employee findById(@PathVariable("id") Long id){  //@PathVariable("id") Long id 从路劲上去获取变量id 赋值给Long id
        return employeeService.findById(id);
    }

    /**
     * 查询所有部门
     * @return
     */
    @ApiOperation(value = "查询所有部门" )
    @GetMapping  //Get  http://localhost/employee
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 修改与添加
     * @param employee
     */
    @ApiOperation(value = "添加与修改部门" )
    @PutMapping  //put http://localhost/employee 对象
    public AjaxResult updateAndAdd(@RequestBody Employee employee){ //@RequestBody取对象使用

            if (employee.getId()==null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
    }

    /**
     * 删除单个部门
     * @param id
     */
    @ApiOperation(value = "通过id删除部门" )
    @DeleteMapping("/{id}")  //delete  http://localhost/employee/1
    public AjaxResult delete(@PathVariable("id") Long id){  //@PathVariable("id") Long id 从路劲上去获取变量id 赋值给Long id

            employeeService.delete(id);
            return AjaxResult.me();
    }

    /**
     * 分页
     * @param employeeQuery
     * @return
     */
    @PostMapping  //post http://localhost/employee 对象
    @ApiOperation("分页查询")
    private PageList<Employee> querypage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation("批量删除对象")
    public AjaxResult patch(@RequestBody Long[] ids){
            employeeService.patch(ids);
            return AjaxResult.me();
    }
    @GetMapping("/list/{id}")
    public List<Employee> Employees(@PathVariable("id") Long id){
        return employeeMapper.findEmployeesById(id);
    }
}
